package com.yanlight.util;

import com.yanlight.app.request.WxCouponRequest;
import com.yanlight.app.response.WxCouponResponse;
import com.yanlight.support.wx.WxConstants;
import com.yanlight.support.wx.util.SignatureUtil;

import java.util.Map;
import java.util.SortedMap;

public class WxCouponUtil {

    public static WxCouponResponse testGrantCoupon(String openId, String appId, String mchId, String apiKey, String certPath) {

        WxCouponResponse wxCouponResponse = null;
        WxCouponRequest request = new WxCouponRequest();

        String nonceStr = RandomStringUtil.randomAlphanumeric(32);
        String partnerTradeNO = mchId + DateTimeUtil.getNowDate() + RandomStringUtil.randomNumeric(24);
        request.setCoupon_stock_id("9548339");
        request.setAppid(appId);
        request.setMch_id(mchId);
        request.setOpenid(openId);
        request.setNonce_str(nonceStr);
        request.setPartner_trade_no(partnerTradeNO);

        SortedMap<String, String> stringStringSortedMap = request.setupParams();
        String apiSignHMAC = SignatureUtil.createApiSign(stringStringSortedMap, apiKey);
        request.setSign(apiSignHMAC);

        String xmlFromObj = XmlParseUtil.getXmlFromObj(request);
        String result = WxHttpClientUtil.xmlPost(WxConstants.WX_COUPON, mchId, certPath, xmlFromObj);
        Map<String, String> stringStringMap = XmlUtil.xmlToMap(result);

        if (stringStringMap == null) {
            return wxCouponResponse;
        }

        try {
            wxCouponResponse = Map2BeanUtil.mapToObject(stringStringMap, WxCouponResponse.class);
        } catch (Exception e) {
            wxCouponResponse = null;
        }
        return wxCouponResponse;
    }

    public static WxCouponResponse grantCoupon(String openId) {

        WxCouponResponse wxCouponResponse = null;
        WxCouponRequest request = new WxCouponRequest();

        String nonceStr = RandomStringUtil.randomAlphanumeric(32);
        String partnerTradeNO = WxConstants.mchId + DateTimeUtil.getNowDate() + RandomStringUtil.randomNumeric(24);
        request.setCoupon_stock_id("9548137");
        request.setAppid(WxConstants.appId);
        request.setMch_id(WxConstants.mchId);
        request.setOpenid(openId);
        request.setNonce_str(nonceStr);
        request.setPartner_trade_no(partnerTradeNO);

        SortedMap<String, String> stringStringSortedMap = request.setupParams();
        String apiSignHMAC = SignatureUtil.createApiSign(stringStringSortedMap, WxConstants.apiKey);
        request.setSign(apiSignHMAC);

        String xmlFromObj = XmlParseUtil.getXmlFromObj(request);
        String result = WxHttpClientUtil.xmlPost(WxConstants.WX_COUPON, WxConstants.mchId, WxConstants.certPath, xmlFromObj);
        Map<String, String> stringStringMap = XmlUtil.xmlToMap(result);

        if (stringStringMap == null) {
            return wxCouponResponse;
        }

        try {
            wxCouponResponse = Map2BeanUtil.mapToObject(stringStringMap, WxCouponResponse.class);
        } catch (Exception e) {
            wxCouponResponse = null;
        }
        return wxCouponResponse;
    }
}
